#include "oled.h"
#include "oledfont.h"

uint8_t OLED_GRAM [128][8];	

void Delay_ms(uint8_t n)
{
  HAL_Delay(n);
}

/**
 * @brief 发送1字节
 **/
void OLED_WB(uint8_t data)
{
  HAL_SPI_Transmit(&hspi1, &data, 1, 1000);
}

/**
 * @brief 写数据
 **/
void OLED_WrDat(uint8_t dat)
{
  OLED_DC_Set;
  OLED_WB(dat);
}

/**
 * @brief 发命令
 **/
void OLED_WrCmd(uint8_t cmd)
{
  OLED_DC_Clr;
  OLED_WB(cmd);
}


/**
 * @brief 从某处开始, 刷新缓存
 * @param page  页数 0~7
 * @param col   列数 0~127
 **/
void OLED_SetPos(uint8_t page, uint8_t col) 
{ 
  OLED_WrCmd(0xb0+page);            // 页
  OLED_WrCmd(0x00|(col&0x0f));      // 低地址
  OLED_WrCmd(0x10|((col&0xf0)>>4)); // 高地址
}


/**
 * @brief 在某处显示字符(不影响其它地方字符显示)
 * @param row 行号; 16号字0~3;  8号字0~7
 * @param col 列号; 16号字0~15; 8号字0~20
 * @param chr 显示字符; 支持部分ASCII字符
 **/
void OLED_DispChar(uint8_t row, uint8_t col, uint8_t chr)
{
  if (OLED_DISP_SIZE==16) {
    row = 3 - row;
    row <<= 1;
    col <<= 3;
    uint8_t index = chr - ' ';
    OLED_SetPos(row, col);
    for (uint8_t k = 0; k < 8; k++)
    {
      OLED_WrDat(F8X16[index][k+8]);
    }
    OLED_SetPos(row+1, col);
    for (uint8_t k = 0; k < 8; k++)
    {
      OLED_WrDat(F8X16[index][k]);
    }
  }
  else {
    row = 7 - row;
    col *= 6;
    uint8_t index = chr - ' ';
    OLED_SetPos(row, col);
    for (uint8_t k = 0; k < 6; k++)
    {
      OLED_WrDat(F6X8[index][k]);
    }
  }
}


/**
 * @brief 从某处开始, 显示字符串
 * @param row 行号; 16号字0~3;  8号字0~7
 * @param col 列号; 16号字0~15; 8号字0~20
 * @param chr 显示字符串; 支持部分ASCII字符
 **/
void OLED_DispString(uint8_t row,uint8_t col,uint8_t *chr)
{
  unsigned char j=0;
  while (chr[j]!='\0')
  {		
    OLED_DispChar(row,col,chr[j]);
    col += 1;
    if(col>MAX_CHAR_COL)
    {
      col=0;
      row+=1;
    }
      j++;
  }
}


/**
 * @brief 刷新全部显存
 **/
void OLED_Refresh_Gram(void)
{
  uint8_t i,n;		    
  for(i=0;i<8;i++)  
  {  
    OLED_WrCmd(0xb0+i); // 页
    OLED_WrCmd(0x00);   // 低地址
    OLED_WrCmd(0x10);   // 高地址
    for(n=0;n<128;n++) 
      OLED_WrDat(OLED_GRAM[n][i]); //写入
  }   
}

/**
 * @brief 满屏
 **/
void OLED_Fill(void)
{
	for(uint8_t row=0; row<8; row++)
	{
		for(uint8_t col=0; col<128; col++) OLED_GRAM[col][row]=0xFF;
	}													    
	OLED_Refresh_Gram();
}


/**
 * @brief 清屏
 **/
void OLED_CLR(void)
{	
	for(uint8_t row=0; row<8; row++)
	{
		for(uint8_t col=0; col<128; col++) OLED_GRAM[col][row]=0x00;
	}													    
	OLED_Refresh_Gram();
}


/**
 * @brief 初始化OLED, 完成后显示hello world~
 **/
void OLED_Init(void)
{
  OLED_RST_Clr;
  Delay_ms(100);
  OLED_RST_Set;
  OLED_WrCmd(0xae);

  OLED_WrCmd(0xD5);
  OLED_WrCmd(0x80);
  OLED_WrCmd(0xA8);
  OLED_WrCmd(0X3F);
  OLED_WrCmd(0xD3);
  OLED_WrCmd(0X00);

  OLED_WrCmd(0x40);
  OLED_WrCmd(0xA1);
  OLED_WrCmd(0xC0);

  OLED_WrCmd(0x8D);
  OLED_WrCmd(0x14);
  OLED_WrCmd(0x20);
  OLED_WrCmd(0x02);
  OLED_WrCmd(0xDA);
  OLED_WrCmd(0x12);

  OLED_WrCmd(0x81);
  OLED_WrCmd(BRIGHTNESS);
  OLED_WrCmd(0xD9);
  OLED_WrCmd(0xf1);
  OLED_WrCmd(0xDB);
  OLED_WrCmd(0x30);

  OLED_WrCmd(0xA4);
  OLED_WrCmd(0xA6);
  OLED_WrCmd(0xAF);
	
  OLED_CLR(); //清屏
  OLED_DispString(1,2,"Hello World ~");
  
}
